import 'package:flutter/material.dart';
import '../navigator/hi_navigator.dart';
import '../http/core/hi_error.dart';
import '../http/dao/login_dao.dart';
import '../http/dao/login_dao.dart';
import '../util/string_util.dart';
import '../util/toast.dart';
import '../widget/appbar.dart';
import '../widget/login_button.dart';
import '../widget/login_effect.dart';
import '../widget/login_input.dart';

///注册页面
import 'package:flutter/material.dart';

class RegistrationPage extends StatefulWidget {

    RegistrationPage({Key key}) : super(key: key);
    _RegistrationPageState createState() => _RegistrationPageState();
}

class _RegistrationPageState extends State<RegistrationPage> {
    bool protect = false;
    bool loginEnable = false;
    String userName;
    String password;
    String rePassword;
    String imoocId;
    String orderId;

    @override
    Widget build(BuildContext context){
        return Scaffold(
            appBar: appBar("注册", "登录", (){
                HiNavigator.getInstance().onJumpTo(RouteStatus.login);
            }),
            body: Container(
                child: ListView(
                    children: [
                        LoginEffect(
                            protect: protect,
                        ),
                        LoginInput(
                            "用户名",
                            "请输入用户名",
                            onChanged: (text){
                                userName = text;
                                checkInput();
                            },
                        ),
                        LoginInput(
                            "密码",
                            "请输入密码",
                            obscureText: true,
                            onChanged: (text){
                                password = text;
                                checkInput();
                            },
                            focusChanged: (focus) {
                                this.setState(() {
                                    protect = focus;
                                });
                            },
                        ),
                        LoginInput(
                            "确认密码",
                            "请再次输入密码",
                            lineStretch: true,
                            obscureText: true,
                            onChanged: (text) {
                                rePassword = text;
                                checkInput();
                            },
                            focusChanged: (focus) {
                                this.setState(() {
                                    protect = focus;
                                });
                            },
                        ),
                        LoginInput(
                            "慕课网ID",
                            "请输入你的慕课网用户ID",
                            keyboardType: TextInputType.number,
                            onChanged: (text) {
                                imoocId = text;
                                checkInput();
                            },
                        ),
                        LoginInput(
                            "课程订单号",
                            "请输入课程订单号后四位",
                            keyboardType: TextInputType.number,
                            lineStretch: true,
                            onChanged: (text) {
                                orderId = text;
                                checkInput();
                            },
                        ),
                        Padding(
                            padding: EdgeInsets.only(top: 20, left: 20, right: 20),
                            child: LoginButton(
                                '注册',
                                enable: loginEnable,
                                onPressed: checkParams
                            ),
                        )
                    ],
                ),
            ),
        );
    }

    void send() async {
        try {
            var result = await LoginDao.registration(userName, password, imoocId, orderId);
            print(result);
            if (result['code'] == 0) {
                print('注册成功');
                showToast('注册成功');
                HiNavigator.getInstance().onJumpTo(RouteStatus.login);
            } else {
                print(result['msg']);
                showWarnToast(result['msg']);
            }
        } on NeedAuth catch (e) {
            print(e);
            showWarnToast(e.message);
        } on HiNetError catch (e) {
            print(e);
            showWarnToast(e.message);
        }
    }

    void checkInput(){
        bool enable;
        if (isNotEmpty(userName) && isNotEmpty(password) && isNotEmpty(rePassword) && isNotEmpty(orderId)){
            enable = true;
        } else {
            enable = false;
        }

        setState(() {
            loginEnable = enable;
        });
    }

    void checkParams() {
        String tips;
        if (password != rePassword) {
            tips = '两次密码不一致';
        } else if (orderId.length != 4) {
            tips = "请输入订单号的后四位";
        }
        if (tips != null) {
            print(tips);
            return;
        }
        send();
    }
}
